package com.zkdcloud.repository;

import com.zkdcloud.domain.Address;
import org.jfaster.mango.annotation.DB;
import org.jfaster.mango.annotation.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 地址dao
 *
 * @author zk123
 * @version 2018/5/22
 */
@DB(table = "address")
@Repository
public interface AddressDao {
    String COLUMN = " id,summary,detail,phone,name,gender,user_id,create_time";

    /**
     * 添加地址
     *
     * @param address 地址
     */
    @SQL("insert into #table ("+COLUMN+") values(:id,:summary,:detail,:phone,:name,:gender,:userId,:createTime)")
    void add(Address address);

    /**
     * 根据uid获取收获地址
     *
     * @return list<Address></>
     */
    @SQL("select * from #table where user_id = :1")
    List<Address> getAllByUid(String uid);

    /**
     * 根据id获取
     *
     * @param id id
     * @return adr
     */
    @SQL("select * from #table where id = :1")
    Address getById(String id);

    /**
     * 根据id删除指定
     *
     * @param id id
     */
    @SQL("delete from #table where id = :1")
    void  removeById(String id);

    /**
     * 更新指定地址
     *
     * @param address 地址
     */
    @SQL("update #table set id = :id" +
            "#if(:summary != null && :summary != '') ,summary = :summary #end" +
            "#if(:detail != null && :detail != '') ,detail = :detail #end" +
            "#if(:phone != null && :phone != '') ,phone = :phone #end" +
            "#if(:name != null && :name != '') ,name = :name #end" +
            "#if(:gender != null && :gender != '') ,gender = :gender #end" +
            " where id = :id")
    void update(Address address);
}
